﻿log("Running Taobao Searcher 2  ...  searchTaobao2.js")



// current position
isIndexPage = function ()
{
    return location.href.search("www.tmall.com") != -1;
}
isSearchPage = function ()
{
    return location.href.search("list.tmall.com/search_product") != -1;
}

// index
getSearchEdit = function ()
{
    return mq;
}
getSubmitButton = function ()
{
    return document.querySelector(".mallSearch-input > button:nth-child(3)");
}

// search methods
priceSetted = function ()
{
    return location.href.search("start_price=") != -1;
}
getSortMethod = function ()
{
    var match = /&sort=([^&]*)/.exec(location.href);
    if (!match)
        return "defaultSort";
    switch (match[1])
    {
        case "s":
            return "defaultSort";
        case "rq":
            return "populartySort";
        case "d":
            return "salesSort";
        case "new":
            return "newSort";
        case "pd":
            return "priceSort";
        case "p":
            return "priceSortAsc";
        default:
            return "defaultSort";
    }
}
locationSetted = function ()
{
    return true;
}

// price 
getItemLowerPriceEdit = function ()
{
    return document.querySelector("#J_FPrice > div.fP-box > b:nth-child(1) > input");
}
getItemHigherPriceEdit = function ()
{
    return document.querySelector("#J_FPrice > div.fP-box > b:nth-child(3) > input");
}
getSubmitPriceButton = function ()
{
    return document.querySelector("#J_FPEnter");
}

// sort by
getSortByDefaultButton = function ()
{
    return document.querySelector("#J_Filter > a.fSort.fSort-cur");
}
getSortBySalesButton = function ()
{
    return document.querySelector("#J_Filter > a:nth-child(4)");
}
getSortByPolulartyButton = function ()
{
    return document.querySelector("#J_Filter > a:nth-child(2)");
}

// location
getLocationButton = function (loc)
{
    var _top = document.querySelector("#J_FDestArea > div");
    var a = _top.querySelectorAll("a");
    for (var i = 0; i < a.length; i++)
    {
        if (a[i].text === loc)
            return a[i];
    }
    return null;
}

// items 
getTops = function ()
{
    return document.querySelectorAll("div.product");
}
getA = function (_top)
{
    return _top.querySelector(".productTitle a");
}
parseTop = function(_top)
{
    var img = _top.querySelector(".productImg img");
    var a = _top.querySelector(".productTitle a");
    var id = a ? a.getAttribute("data-id") : null;
    var price = _top.querySelector(".productPrice");
    var shop = _top.querySelector(".productShop");
    var _location = null;

    return {
        extra: {
            top: _top
        },
        title: a ? fixString(a.textContent) : "",
        id: id ? id : "",
        price: price ? fixPrice(fixString(price.textContent)) : "",
        shop: shop ? fixString(shop.textContent) : "",
        link: a ? a.href : "",
        imgsrc: https2http(img ? ((img.src && img.src) != "" ? img.src : ("http:" + img.getAttribute("data-src"))) : ""),
        location: ""
    };
}

nextPageButton = function ()
{
    return document.querySelector(".ui-page-next");
}